#include "Ticker.h"
#include "rtc.h"
#include "sht.h"
#include "disp.h"
#include "ui.h"
#include "pixelwifi.h"
#include "weather.h"
#include "touch.h"

// extern uint8_t firmware_run;
Ticker cnt_sch;
uint16_t task_1ms = 0, task_10ms = 0, task_100ms = 0, task_400ms = 0, task_500ms = 0, task_1s = 0, task_5s = 0;

void cntHandle(void)
{
    task_1ms++;
    task_10ms++;
    task_100ms++;
    task_400ms++;
    task_500ms++;
    task_1s++;
    task_5s++;
}

// void _weather_task(void *para)
// {
//     while(1)
//     {
//         weatherTask();
//         vTaskDelay(1000);
//     }
// }

// void _wifi_task(void *para)
// {
//     while(1)
//     {
//         wifiCheckTask();
//         vTaskDelay(5000);
//     }
// }

void taskStart(void)
{
    cnt_sch.attach_ms(1, cntHandle);
}

void taskStop(void)
{
    cnt_sch.detach();
}

void taskRun(void)
{
    if(task_10ms >= 10)
    {
        // Serial.println("ui task");
        uiTask();
        task_10ms = 0;
    }
    if(task_100ms >= 100)
    {
        // Serial.println("analog task");
        brightControlTask();
        // touchScan();
        task_100ms = 0;
    }
    if(task_400ms >= 400)
    {
        task_400ms = 0;
    }
    if(task_500ms >= 500)
    {
        // Serial.println("rtc task");
        rtcTask();
        task_500ms = 0;
    }
    if(task_1s >= 1000)
    {
        // Serial.println("weather task");
        ntpTask();
        shtTask();
        // weatherTask();
        task_1s = 0;
    }
    if(task_5s >= 5000)
    {
        wifiCheckTask();
        task_5s = 0;
    }
}
